package com.ebo.common.smart;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Data;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 地址数据加载器
 */
public interface AddressDataLoader {

    List<Address> loadData();

    @Data
    class Address {
        /*  private String id;
          private String name;
          private String code;
          private Serializable parentId;
          private List<Address> children;*/


        private String id;
        private String name;
        private String code;
        private String zipcode;
        private Serializable parentId;
        private List<Address> children;
    }


    public static void main(String[] args) {
/*
        //--写数据扁平化  start
        String jsonstrLocal = FileUtil.readString(new File("D:\\77.txt"),"UTF-8");
        List<Address> list77 = JSONUtil.parseArray(jsonstrLocal).toList(Address.class);

        List<Address> toAddList = Lists.newArrayList();
     for (Address address : list77) {
            List<Address> addresses = flattenTree(address);
            toAddList.addAll(addresses);
        }

        String jsonStr = JSONUtil.toJsonStr(toAddList);
        FileUtil.writeBytes(jsonStr.getBytes(Charset.defaultCharset()) ,new File("D:\\88.txt"));*/
        //--写数据扁平化 end



        //--数据修复结构 parentid  start
 /*       String jsonstr = FileUtil.readString(new File("D:\\66.txt"),"UTF-8");

        List<Address> list66 = JSONUtil.parseArray(jsonstr).toList(Address.class);
//        System.out.println("list66 = " + JSONUtil.toJsonStr(list66));
        for (Address address : list66) {
            flattenTreeWrite(address);
        }

        String jsonstr99组装的数据 = JSONUtil.toJsonStr(list66);
        FileUtil.writeBytes(jsonstr99组装的数据.getBytes(Charset.defaultCharset()) ,new File("D:\\99组装的数据.txt"));

        System.out.println("toAddSets = " );*/
        //省份和市级数据提取
        List<String> strings = FileUtil.readLines(new File("D:\\省份和市级数据提取.txt"), "UTF-8");
        List<String> collect = strings.stream().filter(i -> i.contains("市") || i.contains("省") || i.contains("自治区") || i.contains("区") || i.contains("州") || i.contains("县")).collect(Collectors.toList());

        String jsonStr = JSONUtil.toJsonStr(collect);
        FileUtil.writeBytes(jsonStr.getBytes(Charset.defaultCharset()) ,new File("D:\\省份和市级数据提取后的数据.txt"));
    }



    public static List<Address> flattenTree(Address root) {
        List<Address> result = new ArrayList<>();
        flatten(root, result);
        return result;
    }

    public static void flatten(Address node, List<Address> result) {
        if (node == null) return;
        List<Address> children = node.getChildren();
        if(!CollectionUtil.isEmpty(children)){
            node.setChildren(null);
            for (Address child : children) {
                flatten(child, result);
            }
        }
        result.add(node);
    }



/*
    //递归tree树
    public static List<Map<String, Object>> doTreeList(List<Map<String, Object>> dataList, String pid) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (dataList != null) {
            for (Map<String, Object> item : dataList) {
                if (pid.equals(item.get("pid").toString())) {
                    List<Map<String, Object>> children = doTreeList(dataList, item.get("id").toString());
                    item.put("children", children);
                    resultList.add(item);
                }
            }
        }
        return resultList;
    }
*/


    public static List<Address> flattenTreeWrite(Address root) {
//        List<Address> result = new ArrayList<>();
        flattenWrite(root/*, result*/);
        return /*result*/null;
    }

    public static void flattenWrite(Address node/*, List<Address> result*/) {
        if (node == null) return;
        String code = node.getCode();
        node.setCode(code);
        node.setId(code);
//        List<Address> children = node.getChildren();
        if(!CollectionUtil.isEmpty(node.getChildren())){
            for (Address child : node.getChildren()) {
                String codeChild = child.getCode();
                child.setParentId(code);
                child.setCode(codeChild);
                child.setId(codeChild);
                flattenWrite(child/*, result*/);
            }
        }
       /* result.add(node);*/
    }






}
